package com.rustic.server.controller.api;

import cn.hutool.core.collection.CollUtil;
import cn.xy.base.server.bean.IPage;
import cn.xy.base.server.utils.PageUtil;
import cn.xy.commons.annotation.ApiAuth;
import com.rustic.server.enums.LinkType;
import com.rustic.server.po.Attractions;
import com.rustic.server.query.AttractionsUserListQuery;
import com.rustic.server.service.AttractionsService;
import com.rustic.server.service.CommentService;
import com.rustic.server.vo.AttractionsWebVO;
import com.rustic.server.vo.list.AttractionsWebListVO;
import com.rustic.server.vo.AttractionsVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rustic.server.web.WebContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[attractions]对应的前台接口
 *
 * @author auto
 */

@Slf4j
@RestController
@RequestMapping("/api/attractions")
@Api(tags = {"景点接口"})
@ApiAuth(ignore = true)
public class AttractionsController {

    @Autowired
    private AttractionsService attractionsService;

    @Autowired
    private CommentService commentService;

    @ApiOperation(value = "景点-列表")
    @GetMapping("/list")
    public Result<Page<AttractionsWebListVO>> list(AttractionsUserListQuery query) {
        LambdaQueryWrapper<Attractions> queryWrapper = attractionsService.toWebListWrapper(query);
        Page<Attractions> pageData = attractionsService.page(query.toPage(), queryWrapper);
        List<Attractions> attractionsList = pageData.getRecords();
        if (CollUtil.isEmpty(attractionsList)) {
            return Result.data(new Page<>());
        }
        int userId = WebContext.getUserId();
        List<AttractionsWebListVO> results = attractionsService.toWebListVo(attractionsList, userId);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "景点-详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "景点id"),
    })
    @GetMapping("/detail")
    public Result<AttractionsWebVO> detail(int id) {
        if (id <= 0) {
            return Result.error(AttractionsService.ATTRACTIONS_NOT_EXIST);
        }
        AttractionsWebVO attractionsVO = attractionsService.getByIdToWeb(id);
        int userId = WebContext.getUserId();
        commentService.setInteractionStatus(attractionsVO, userId, attractionsVO.getId(), LinkType.ATTRACTIONS);
        return Result.data(attractionsVO);
    }

}
